#! /usr/bin/perl

#also enable strict mode
use strict;

#use the HMTLSubs package and DBQueries package
use HTMLSubs;
use CGI;
use DBQueries;

#create new cgi object
my $CGIHandle = new CGI;

#ensure that input is not blank which would mean that the site is illegally accessed
if(scalar($CGIHandle->param) == 0){
	
	#if so, redirect to login
	print CGI -> redirect("login.pl.cgi");
}


#get parameters
my $username = $CGIHandle -> param('username');
my $group_name = $CGIHandle -> param('groupName') || "";
my @user_names = $CGIHandle -> param('usersToDelete');

#declare display title to send correct info to user
my $display_message = "";

#determine if there was a user passed back to the script
if(scalar(@user_names) != 0){
	
	#loop through ids and take appropriate action
	my $current_user_name;
	
	foreach $current_user_name(@user_names){
		
		#add to database
		DBQueries::removeGroupMember($current_user_name, $group_name);
		
		#create title for user
		$display_message .= "Removed $current_user_name from $group_name successfully!<br>";
	}
}
#otherwise, no file was selected so act accordingly
else{
	#create title for user
	$display_message = "No user was selected!";
}

#call sub to start html document
HTMLSubs->generate_html_beginning();
	
#call sub to print header
	HTMLSubs -> generate_display_head("Document Management System", "Remove Group Users", $username);
	
	print <<END_HTML;

<!--now display the message to the user-->
<h4>You removed the following members:<br>$display_message</h4>
<h4>Please return to your profile or user manager for further action.</h4>
<br>

END_HTML

#create hash for button
my %parameter_hash;
$parameter_hash{'username'} = $username;
$parameter_hash{'groupName'} = $group_name;
	
#call subroutine to display button that returns the user to the profile
HTMLSubs -> generate_html_button("profile.pl.cgi", "Go To profile", \%parameter_hash);

#call subroutine to display button that returns the user to user manager
HTMLSubs -> generate_html_button("manageGroupUsers.pl.cgi", "Go To User Manager", \%parameter_hash);

#now call method to produce closing html tags
HTMLSubs->generate_html_end();
